#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct Stu {
	char name[30];
	int age;
};

int struct_cmp_by_name(const void* e1, const void* e2)
{
	return strcmp((*(struct Stu*)e1).name, (*(struct Stu*)e2).name);
}

void Swap(char* x, char* y, size_t width)
{
	int i = 0;
	
	for (i = 0; i < width; i++)
	{
		char tmp = *x;
		*x = *y;
		*y = tmp;
		x++;
		y++;
	}
}

void bubble_sort(void* base, size_t num, size_t width, int (*cmp)(const void* e1, const void* e2))
{
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
		int j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
			if(cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
				Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
		}
	}
}

int main()
{
	struct Stu s[3] = { {"zhangsan", 26} , {"lisi", 12} , {"wangwu" , 54} };
	int sz = sizeof(s) / sizeof(s[0]);
	bubble_sort(s, sz, sizeof(s[0]), struct_cmp_by_name);
	return 0;
}